package com.client.dbdao;

import com.data.dbdao.BaseDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 二级界面的处理Dao
 *
 * @author:HingLo
 * @create 2017-10-10 15:53
 **/
@Repository
@Transactional
public class SoftwareDetailClientDao extends BaseDao {
    private static Logger logger = Logger.getLogger(SoftwareDetailClientDao.class);

    public List<?> findSoftwareDetailDao(String id, String types) {
        String hql = "select rid ,name from AppIcon where rid=? and types=?";
        return super.findMassageDao(hql, id, types);
    }

    public List<?> lookSpeakInfoDao(String rid, int start, int end) {
        String hql = "SELECT s.id, a.username  as name,s.contents,s.ctime,s.star,s.good from Account as a ,Speaking as s WHERE a.id=s.uid and rid=? ORDER BY s.ctime DESC limit ?,?";
        return super.findMysqlMethodDao(hql, rid, start, end);
    }

    public boolean updateSpeakingGoodDao(String id) {
        String hql = "update Speaking set good=good+1 where id=?";
        return super.updateOneMssageDao(hql, id);
    }

    public List<?> findCollectionSoftwareDao(String uid) {
        String hql = "select a.id,a.name ,a.introduce,m.name,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.id in (select rid from CollectionSoftware where uid=?)";
        return super.findMassageDao(hql, uid);
    }

    /**
     * 模糊查询
     *
     * @param search
     * @return
     */
    public List<?> searchSoftwareDao(String search) {
        String sql = "select a.id,a.name ,a.introduce,m.name as menu,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.name like ";
        search = "'%" + search + "%'";
        sql += search;
        return super.findMysqlMethodDao(sql);
    }

    public List<?> findCollectionSoftwareDao(String uid, String rid) {
        String hql = "from CollectionSoftware where uid=? and rid=?";
        return super.findMassageDao(hql, uid, rid);
    }

    public List<?> searchTagsSoftwareDao(int len) {
        String hql = "from Tag";
        return super.findMassageDao(hql);
    }

    public boolean deleteCollectionSoftwareDao(String uid, String... rid) {
        logger.debug(rid);
        logger.debug(uid);
        String[] param = new String[rid.length + 1];
        param[0] = uid;
        StringBuilder hql = new StringBuilder("delete CollectionSoftware where uid=? and rid in (");
        for (int i = 1; i < param.length; i++) {
            hql.append("?,");
            param[i] = rid[i - 1];
        }
        int len = hql.length();
        hql.delete(len - 1, len);
        hql.append(")");
        logger.debug(hql.toString());

        return super.updateManyMssageDao(hql.toString(), param) > 0;
    }


    public List<?> updateAdminSoftwareDao(String[] param, boolean bool) {
        StringBuilder hql;
        if (bool) {
            hql = new StringBuilder("select a.id,a.name ,a.introduce,m.name,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.packagename in (");

        } else {
            hql = new StringBuilder("select a.id,a.name ,a.introduce,m.name,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.id in (");

        }


        for (String aParam : param) {
            hql.append("?,");
        }
        int len = hql.length();
        hql.delete(len - 1, len);
        hql.append(")");
        return super.findMassageDao(hql.toString(), param);
    }

    public List<?> speakLookDao(String rid) {
        String sql = "SELECT star,COUNT(id) FROM speaking where rid=? GROUP BY star";
        return super.findMysqlMethodDao(sql, rid);
    }

    /**
     * 模糊搜索实现
     *
     * @param search
     * @return
     */
    public List<?> searchTagSoftwareDao(String search) {
        //默认搜索最多是40条数据
        StringBuilder sql = new StringBuilder("select a.id,a.name ,a.introduce,m.name as mname,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.id in (SELECT rid from appmenu where mid in(SELECT mid from appmenu where  rid in(SELECT id from app where name LIKE ");
        sql.append("'%").append(search).append("%'").append(")))  and a.id not in(select id from app where name like ").append("'%").append(search).append("%'").append(") limit 0,40");
        return super.findMysqlMethodDao(sql.toString());
    }

    /**
     * 详情页的软件推荐列表
     *
     * @param id
     * @param len
     * @return
     */
    public List<?> recommendSoftwareDao(String id, int len) {
        String sql = "select a.id,a.name ,a.introduce,m.name as mname,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.id in (SELECT rid from appmenu where  mid in(SELECT mid from appmenu where rid=?)) limit 0,?";


        return super.findMysqlMethodDao(sql, id, len);
    }
}
